package com.cy.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.cy.entity.BackRole;
import com.cy.entity.BackUser;
import com.cy.entity.BackUserRole;

@Component("backUserRoleDao")
public class BackUserRoleDao extends BaseDAOHibernate<BackUserRole>{

	public void deleteBackUserRoleByUser(BackUser backUser){
		Session session=this.getSession();
		StringBuffer hql=new StringBuffer();
		hql.append(" DELETE FROM BackUserRole br WHERE br.backUser=:backUser ");
		Query query=session.createQuery(hql.toString());
		query.setParameter("backUser", backUser);
		query.executeUpdate();
	}
	
	public void deleteBackUserRoleByRole(BackRole backRole){
		Session session=this.getSession();
		StringBuffer hql=new StringBuffer();
		hql.append(" DELETE FROM BackUserRole br WHERE br.role=:role ");
		Query query=session.createQuery(hql.toString());
		query.setParameter("role", backRole);
		query.executeUpdate();
	}
	
	public List<BackUserRole> listBackUserRoleByBackUser(BackUser backUser){
		List<BackUserRole> backUserRoles=new ArrayList<BackUserRole>();
		Session session=this.getSession();
		StringBuffer hql=new StringBuffer();
		hql.append(" FROM BackUserRole br WHERE br.backUser=:backUser");
		Query query=session.createQuery(hql.toString());
		query.setParameter("backUser", backUser);
		backUserRoles=query.list();
		return backUserRoles;
	}
}
